package cn.com.hiocde.myresume.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import cn.com.hiocde.myresume.R;

public class ScaleImage extends Activity implements View.OnTouchListener{

    private ImageView img_view;
    private  float currentDistance=0;
    private float lastDistance=-1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_scale_image);

        Intent intent=getIntent();
        int img_id=intent.getIntExtra("img_id",-1);
        img_view=(ImageView)findViewById(R.id.big_img);
        img_view.setImageResource(img_id);

        img_view.setOnTouchListener(this);
    }

    public boolean onTouch(View v, MotionEvent event){

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN: //触摸按下
                //Log.i("MotionEvent","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE: //触摸移动
                //Log.i("MotionEvent","ACTION_MOVE");

                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) img_view.getLayoutParams();

                if(event.getPointerCount()==1) {
                    //单个触摸点，并拖动图片
                    System.out.println(String.format("X:%f,Y:%f", event.getX(), event.getY()));

                    layoutParams.leftMargin = (int) event.getX()-10;
                    layoutParams.topMargin = (int) event.getY()-10;

                    img_view.setLayoutParams(layoutParams);
                }

                //System.out.println("pointer count 触摸点个数"+event.getPointerCount());//触摸点个数
                //多个触摸点的坐标


                //手势放大缩小图片
                if (event.getPointerCount()>=2){
                    System.out.println(String.format("多个触摸点的坐标X1:%f;Y1:%f", event.getX(0), event.getX(0)));
                    float offsetX=event.getX(0)-event.getX(1);
                    float offsetY=event.getY(0)-event.getY(1);
                    currentDistance=(float)Math.sqrt(offsetX*offsetX+offsetY*offsetY);

                    if(lastDistance<0){
                        lastDistance=currentDistance;
                    }else {
                        if(currentDistance-lastDistance>5){    //5px;有误差
                            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) img_view.getLayoutParams();
                            //System.out.println("放大");
                            lp.width= (int) (1.1f*img_view.getWidth());
                            lp.height=(int) (1.1f*img_view.getHeight());
                            img_view.setLayoutParams(lp);
                            lastDistance=currentDistance;  //放大
                        }else if (lastDistance-currentDistance>5){
                            lastDistance=currentDistance;    //缩小
                            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) img_view.getLayoutParams();
                            lp.width= (int) (0.9f*img_view.getWidth());
                            lp.height=(int) (0.9f*img_view.getHeight());
                            img_view.setLayoutParams(lp);
                        }

                    }
                }
                break;
            case MotionEvent.ACTION_UP: //触摸弹起
                //Log.i("MotionEvent","ACTION_UP");
                break;
        }

        return true;//true:需要催发后续事件；false：不需要须发后续事件(只能执行一次)
    }

}
